function UpgradeString(upgrade)
		if upgrade == 0 then
			upgradestring = ""
		elseif upgrade == 1 then
			upgradestring = "I"
		elseif upgrade == 2 then
			upgradestring = "II"
		elseif upgrade == 3 then
			upgradestring = "III"
		elseif upgrade == 4 then
			upgradestring = "IV"
		end
	return upgradestring
end

function ShowIventory()
	local DermaPanel = vgui.Create("DFrame") 
  
	DermaPanel:SetPos(((ScrW() - 750) * 0.5),((ScrH() - 600) * 0.5))
	DermaPanel:SetSize(750, 600)
	DermaPanel:SetTitle("Inventory")
	DermaPanel:SetVisible(true) 
	DermaPanel:SetDraggable(false)
	DermaPanel:ShowCloseButton(true)
	DermaPanel:MakePopup() 
	
	PropertySheet = vgui.Create("DPropertySheet")
	PropertySheet:SetParent(DermaPanel)
	PropertySheet:SetPos(5, 30)
	PropertySheet:SetSize(740, 562)
	
------------------------------------
-- Backpack Contents
------------------------------------
	local PlyBackpack = vgui.Create("DPanelList")
	PlyBackpack:EnableHorizontal(true)
	PlyBackpack:EnableVerticalScrollbar(true)
	PlyBackpack:SetPadding(5)
	PlyBackpack:SetSpacing(5)
	PlyBackpack:SetSize(PropertySheet:GetWide(), PropertySheet:GetTall())
	PlyBackpack:SetPos(0, 0)
	local Icon2 = {}
	for k, v in ipairs(ITEMS) do
		local name
		if (v[3] == false) then 
			local sent = scripted_ents.Get(v[1])
			name = sent.PrintName
		else
			local swep = weapons.Get(v[1])
			name = swep.PrintName
		end
		
		Icon2[v] = vgui.Create("SpawnIcon", self)
		Icon2[v]:SetModel(v[2])					
		Icon2[v]:SetToolTip(name)
		Icon2[v].DoClick = function() surface.PlaySound("ui/buttonclickrelease.wav") RunConsoleCommand("backpack_drop", v[1]) Icon2[v]:Remove() end
		
		PlyBackpack:AddItem(Icon2[v])	
	end

	local help = vgui.Create("DLabel", PlyBackpack)
	help:SetText("To place a gun or item in your backpack, bind a key to the console command +backpack \nAlterntively you can use the chat command /backpack to do the same. \nYou must be looking at the item you wish to put into your backpack.")
	help:SetTextColor(color_white)
	help:SizeToContents()
	help:SetPos(10, 480)
	
	local button = vgui.Create("DButton", PlyBackpack)
	button:SetSize(100, 20)
	button:SetPos(620, 500)
	button:SetText("Empty Backpack")
	button.DoClick = function() surface.PlaySound("ui/buttonclickrelease.wav") RunConsoleCommand("say", "/dropbackpack") for k,v in pairs(Icon2) do Icon2[k]:Remove() end end

	PropertySheet:AddSheet("Backpack", PlyBackpack, "gui/silkicons/box", false, false, "Backpack")
	
------------------------------------
-- Inventory
------------------------------------
	local PlyInventory = vgui.Create("DPanelList")
	PlyInventory:EnableHorizontal(true)
	PlyInventory:EnableVerticalScrollbar(true)
	PlyInventory:SetPadding(5)
	PlyInventory:SetSpacing(5)
	PlyInventory:SetSize((PropertySheet:GetWide() - 10), PropertySheet:GetTall())
	PlyInventory:SetPos(0, 0)
	local Icon = {}
	for k, v in pairs(LocalPlayer():GetWeapons()) do
		if (v.ClassName && v.ClassName != "gmod_tool") then 
			Icon[v] = vgui.Create("SpawnIcon", self)
			local mdl
			if(v.WorldModel == nil) then
				mdl = "models/error.mdl"
			else
				mdl = v.WorldModel
			end
			local class = v:GetClass()
			Icon[v]:SetModel(mdl)					
			Icon[v]:SetToolTip(v:GetPrintName())
			Icon[v].DoClick =  function() surface.PlaySound("ui/buttonclickrelease.wav") RunConsoleCommand("say", "/drop ".. class) Icon[v]:Remove() end
			//Icon[v].DoClick =  function()
			//	local class = v:GetClass()
			//	local menu = DermaMenu()
			//	menu:AddOption("Drop",function() surface.PlaySound("ui/buttonclickrelease.wav") RunConsoleCommand("use", class) RunConsoleCommand("say", "/drop") Icon[v]:Remove() end)
			//	menu:AddOption("Move to Backpack",function() surface.PlaySound("ui/buttonclickrelease.wav") RunConsoleCommand("backpack_move", class) Icon[v]:Remove() end)
			//	menu:Open()
			//end
			PlyInventory:AddItem(Icon[v])
		end
	end
	
	local button = vgui.Create("DButton", PlyInventory)
	button:SetSize(120, 20)
	button:SetPos(600, 500)
	button:SetText("Drop All Weapons")
	button.DoClick = function() surface.PlaySound("ui/buttonclickrelease.wav") RunConsoleCommand("say", "/dropall") for k,v in pairs(Icon) do Icon[k]:Remove() end end
	
	PropertySheet:AddSheet("Inventory", PlyInventory, "gui/silkicons/user", false, false, "Inventory")
	
------------------------------------
-- SENTS owned by the player.
------------------------------------
	local PlyInventory = vgui.Create("DPanelList")
	PlyInventory:EnableHorizontal(false)
	PlyInventory:EnableVerticalScrollbar(true)
	PlyInventory:SetPadding(5)
	PlyInventory:SetSpacing(5)
	PlyInventory:SetSize((PropertySheet:GetWide() - 10), PropertySheet:GetTall())
	PlyInventory:SetPos(0, 0)
	for k, v in pairs(ents.GetAll()) do
		if (v.Base == "base_structure" && v.dt.Owner == LocalPlayer()) then
			local GunPanel = vgui.Create('DPanel')
			GunPanel:SetParent(FactoryList)
			GunPanel:SetSize(250, 62)		
			
			local Icon = vgui.Create("SpawnIcon", self)
			Icon:SetModel(v.Model)	
			Icon:SetToolTip(v.PrintName)
			Icon:SetParent(GunPanel)
									
			local pricelabel  = vgui.Create("DLabel", Icon)
			pricelabel:SetFont("TabLarge")
			pricelabel:SetTextColor(Color(200, 200, 0, 255))
			pricelabel:SetText(UpgradeString(v.dt.Upgrade))
			pricelabel:SetContentAlignment(9)
			pricelabel:SetWide(Icon:GetWide())
			
			local PrintName = vgui.Create('DLabel')
			PrintName:SetParent(GunPanel)
			PrintName:SetPos(70, 10)
			PrintName:SetFont("TabLarge")
			PrintName:SetText(v.PrintName)
			PrintName:SizeToContents()

			local Spawned = vgui.Create('DLabel')
			Spawned:SetParent(GunPanel)
			Spawned:SetPos(70, 25)
			Spawned:SetText("Spawned: " .. LocalPlayer():StructCount(v:GetClass()) .. "/"  .. v.MaxStructs)
			Spawned:SizeToContents()
			
			local Health = vgui.Create('DLabel')
			Health:SetParent(GunPanel)
			Health:SetPos(70, 40)
			Health:SetText("Health: " .. v.dt.Health .. "/"  .. v.HP )
			Health:SizeToContents()

			PlyInventory:AddItem(GunPanel)
		end
	end
		
	PropertySheet:AddSheet("Structures", PlyInventory, "gui/silkicons/brick_add", false, false, "Structures")
end	

function InventoryIncoming(handler, id, encoded, decoded)
	ITEMS =  decoded[1]
	ShowIventory()
end
datastream.Hook("ShowInventorySteam", InventoryIncoming)